package com.zbkj.crmeb.store.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.taobao.pac.sdk.cp.PacClient;
import com.taobao.pac.sdk.cp.SendSysParams;
import com.taobao.pac.sdk.cp.dataobject.request.GLOBAL_SALE_ORDER_NOTIFY.*;
import com.taobao.pac.sdk.cp.dataobject.response.GLOBAL_SALE_ORDER_NOTIFY.GlobalSaleOrderNotifyResponse;
import com.utils.HttpUtils;
import com.zbkj.crmeb.enums.BarCodeEnum;
import com.zbkj.crmeb.enums.PurchaseOrderChannelTypeEnum;
import com.zbkj.crmeb.order.AbstractStoreOrderService;
import com.zbkj.crmeb.order.channel.XiYangYangStoreOrderServiceImpl;
import com.zbkj.crmeb.order.response.TraceSourceStoreOrderResponse;
import com.zbkj.crmeb.order.response.XiYangYangOrderResponse;
import com.zbkj.crmeb.store.dao.StoreOrderDao;
import com.zbkj.crmeb.store.dao.StoreOrderInfoDao;
import com.zbkj.crmeb.store.model.*;
import com.zbkj.crmeb.store.request.PushOrderRequest;
import com.zbkj.crmeb.store.response.StoreCartResponse;
import com.zbkj.crmeb.store.service.CainiaoStoreOrderService;
import com.zbkj.crmeb.store.service.OrderInfoDeliveryService;
import com.zbkj.crmeb.store.service.StoreProductDescriptionService;
import com.zbkj.crmeb.store.service.TraceSourceStoreOrderService;
import com.zbkj.crmeb.store.vo.StoreOrderInfoVo;
import com.zbkj.crmeb.store.vo.TSStoreOrderInfoVo;
import com.zbkj.crmeb.system.model.SystemAdmin;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 溯源订单
 * @author daigb
 * @date:  2023-02-02 15:14
 */
@Slf4j
@Service
public class TraceSourceStoreOrderServiceImpl implements TraceSourceStoreOrderService {


	private static String url = "http://api.fushangma.cn:8081/orderApi/pushSuYunOrderInfo"; //联调
	private static Logger logger = LoggerFactory.getLogger(TraceSourceStoreOrderServiceImpl.class);

	@Autowired
	private StoreProductDescriptionService storeProductDescriptionService;

	@Resource
	private StoreOrderInfoDao storeOrderInfoDao;

	@Autowired
	private OrderInfoDeliveryService orderInfoDeliveryService;

	/**
	 * 连接超时 毫秒
	 */
	private static final Integer CONNECT_TIME_OUT = 5000;
	/**
	 * 响应超时 毫秒
	 */
	private static final Integer RESP_TIME_OUT = 30000;

	/**
	 * 订单相关属性赋值
	 *
	 * @author daigb
	 * @date: 2022-10-18 13:43
	 */
	public void pushOrder(StoreOrder storeOrder, StoreOrderInfo info, StoreProductAttrValue value, StoreProduct product, PushOrderRequest pushOrderRequest) {
		//解析商品详情JSON
		TSStoreOrderInfoVo storeOrderInfoVo = new TSStoreOrderInfoVo();

		storeOrderInfoVo.setId(info.getId());
		storeOrderInfoVo.setProductId(product.getId());

		storeOrderInfoVo.setChildOrderNumber(info.getChildOrderNumber() + "-" + info.getId());
		storeOrderInfoVo.setName(product.getStoreName());

		storeOrderInfoVo.setBarcode(product.getBarCode());
		storeOrderInfoVo.setOriginCountry("");

		StoreProductDescription storeProductDescription = storeProductDescriptionService.getOne(Wrappers.<StoreProductDescription>lambdaQuery()
				.eq(StoreProductDescription :: getProductId, product.getId())
				.last("LIMIT 1")
		);

		// 配对商品详情
		if(ObjectUtil.isNotNull(storeProductDescription)) {
			storeOrderInfoVo.setImg(storeProductDescription.getImage());
			storeOrderInfoVo.setImgs(storeProductDescription.getSliderImage());
		}

		storeOrder.setPushOrderNo(info.getChildOrderNumber() + "-" + info.getId());

		storeOrderInfoVo.setOrderId(storeOrder.getOrderId());
		storeOrderInfoVo.setCategoryId(product.getCateId());
		storeOrderInfoVo.setBrandId(product.getBrandId());
		storeOrderInfoVo.setSpecs(value.getAttrValue());
		storeOrderInfoVo.setQrCode(pushOrderRequest.getQrCode());

		//快递信息
		List<OrderInfoDelivery> deliveryLists = orderInfoDeliveryService.list(Wrappers.<OrderInfoDelivery>lambdaQuery()
				.eq(OrderInfoDelivery::getOrderInfoId, info.getId()));

		storeOrderInfoVo.setDeliveryLists(deliveryLists);

		String result = null;
		TraceSourceStoreOrderResponse response = null;
		try {
			logger.info("--------------- 开始请求溯源数据创建订单url: {},body:{} ---------------", url, JSONObject.toJSONString(storeOrderInfoVo));
			result = HttpUtils.post(url,JSONObject.toJSONString(storeOrderInfoVo),RESP_TIME_OUT,CONNECT_TIME_OUT);
			logger.info("--------------- 开始请求溯源数据创建订单返回结果: {} ---------------", result);
			response = JSONObject.parseObject(result, TraceSourceStoreOrderResponse.class);
			//推送成功
			if(null!=response && response.getCode()==1){

				StoreOrderInfo storeOrderInfoUpdate = new StoreOrderInfo();
				storeOrderInfoUpdate.setId(info.getId());
				storeOrderInfoUpdate.setIsPushTs(true);
				storeOrderInfoUpdate.setQrCode(pushOrderRequest.getQrCode());
				storeOrderInfoDao.updateById(storeOrderInfoUpdate);

			}else
			{
				StoreOrderInfo storeOrderInfoUpdate = new StoreOrderInfo();
				storeOrderInfoUpdate.setId(info.getId());
				storeOrderInfoUpdate.setIsPushTs(false);
				storeOrderInfoUpdate.setQrCode(pushOrderRequest.getQrCode());
				storeOrderInfoDao.updateById(storeOrderInfoUpdate);

				logger.info("--------------- 推送溯源数据创建订单url失败返回: {} ---------------", response.getMessage());
			}

		} catch (Exception e) {
			logger.info("--------------- 请求溯源数据创建订单url失败结果: {} ---------------", e);
		}

	}
    
}
